package com.mzba.zxdlyllp.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

import android.content.Context;

import com.mzba.zxdlyllp.db.BookTable;
import com.mzba.zxdlyllp.db.BookTable.BookEntity;
import com.mzba.zxdlyllp.db.MasturbationTable;
import com.mzba.zxdlyllp.db.MasturbationTable.MasturbationEntity;
import com.mzba.zxdlyllp.db.SexTable;
import com.mzba.zxdlyllp.db.SexTipsTable;
import com.mzba.zxdlyllp.db.SexTipsTable.SexTipsEntity;
import com.mzba.zxdlyllp.db.SexualityTable;
import com.mzba.zxdlyllp.db.SexualityTable.SexualityEntity;

/**
 * 
 * @author 06peng
 *
 */
public class DownloadUtils {

	public static void download(int pos, Context context) {
		try {
			String result = null;
			switch (pos) {
			case 0:
				result = HttpUtils.doGet(AppContext.load_love_url);
				if (StringUtil.isNotBlank(result)) {
					String booksUrl[] = result.split(",");
					for (final String url : booksUrl) {
						saveSexPosition(url, context);
					}
				}
				break;
			case 1:
				result = HttpUtils.doGet(AppContext.load_masturbation_url);
				if (StringUtil.isNotBlank(result)) {
					String booksUrl[] = result.split(",");
					for (final String url : booksUrl) {
						saveMasturbation(url, context);
					}
				}
				break;
			case 2:
				result = HttpUtils.doGet(AppContext.load_books_url);
				if (StringUtil.isNotBlank(result)) {
					String booksUrl[] = result.split(",");
					for (final String url : booksUrl) {
						saveBooks(url, context);
					}
				}
				break;
			case 3:
				result = HttpUtils.doGet(AppContext.load_sexuality_url);
				if (StringUtil.isNotBlank(result)) {
					String booksUrl[] = result.split(",");
					for (final String url : booksUrl) {
						saveSexuality(url, context);
					}
				}
				break;
			case 4:
				result = HttpUtils.doGet(AppContext.load_sextips_url);
				if (StringUtil.isNotBlank(result)) {
					String booksUrl[] = result.split(",");
					for (final String url : booksUrl) {
						saveSexTips(url, context);
					}
				}
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void saveSexPosition(String path, Context context) throws Exception {
		SexTable st = new SexTable(context);
		BookEntity entity = new BookTable(context).new BookEntity();
		InputStream inStream = null;
		HttpGet httpRequest = new HttpGet(path);
		HttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 6000);
		HttpResponse httpResponse = httpClient.execute(httpRequest);
		
		if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			inStream = httpResponse.getEntity().getContent();
			StringBuilder sb = new StringBuilder();
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "gbk"));
				String line = null;
				String title = reader.readLine();
				entity.setTitle(title);
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
				byte[] content = sb.toString().getBytes();
				entity.setContent(content);
				st.save(entity);
			} catch (IOException e) {
				sb.delete(0, sb.length());
			} finally {
				HttpUtils.closeStream(inStream);
			}
		} else {
			throw new Exception("conn exception");
		}
	}
	
	private static void saveMasturbation(String path, Context context) throws Exception {
		MasturbationTable bt = new MasturbationTable(context);
		MasturbationEntity entity = new MasturbationTable( context).new MasturbationEntity();
		InputStream inStream = null;
		HttpGet httpRequest = new HttpGet(path);
		HttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 6000);
		HttpResponse httpResponse = httpClient.execute(httpRequest);
		
		if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			inStream = httpResponse.getEntity().getContent();
			StringBuilder sb = new StringBuilder();
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "gbk"));
				String line = null;
				String str = reader.readLine();
				System.out.println("+++++++++++++" + str);
				String titles[] = str.split("\\|");
				entity.setTitle(titles[0]);
				entity.setType(Integer.parseInt(titles[1]));
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
				byte[] content = sb.toString().getBytes();
				entity.setContent(content);
				bt.save(entity);
			} catch (IOException e) {
				sb.delete(0, sb.length());
			} finally {
				HttpUtils.closeStream(inStream);
			}
		} else {
			throw new Exception("conn exception");
		}
	}

	private static void saveBooks(String path, Context context) throws Exception {
		BookTable bt = new BookTable(context);
		BookEntity entity = new BookTable(context).new BookEntity();
		InputStream inStream = null;
		HttpGet httpRequest = new HttpGet(path);
		HttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 6000);
		HttpResponse httpResponse = httpClient.execute(httpRequest);
		
		if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			inStream = httpResponse.getEntity().getContent();
			StringBuilder sb = new StringBuilder();
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "gbk"));
				String line = null;
				String title = reader.readLine();
				entity.setTitle(title);
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
				byte[] content = sb.toString().getBytes();
				entity.setContent(content);
				bt.save(entity);
			} catch (IOException e) {
				sb.delete(0, sb.length());
			} finally {
				HttpUtils.closeStream(inStream);
			}
		} else {
			throw new Exception("conn exception");
		}
	}
	
	private static void saveSexuality(String path, Context context) throws Exception {
		SexualityTable bt = new SexualityTable(context);
		SexualityEntity entity = new SexualityTable(context).new SexualityEntity();
		InputStream inStream = null;
		HttpGet httpRequest = new HttpGet(path);
		HttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 6000);
		HttpResponse httpResponse = httpClient.execute(httpRequest);
		
		if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			inStream = httpResponse.getEntity().getContent();
			StringBuilder sb = new StringBuilder();
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "gbk"));
				String line = null;
				String title = reader.readLine();
				entity.setTitle(title);
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
				byte[] content = sb.toString().getBytes();
				entity.setContent(content);
				bt.save(entity);
			} catch (IOException e) {
				sb.delete(0, sb.length());
			} finally {
				HttpUtils.closeStream(inStream);
			}
		} else {
			throw new Exception("conn exception");
		}
	}
	
	private static void saveSexTips(String path, Context context) throws Exception {
		SexTipsTable bt = new SexTipsTable(context);
		SexTipsEntity entity = new SexTipsTable(context).new SexTipsEntity();
		InputStream inStream = null;
		HttpGet httpRequest = new HttpGet(path);
		HttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 6000);
		HttpResponse httpResponse = httpClient.execute(httpRequest);
		
		if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			inStream = httpResponse.getEntity().getContent();
			StringBuilder sb = new StringBuilder();
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "gbk"));
				String line = null;
				String title = reader.readLine();
				entity.setTitle(title);
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
				byte[] content = sb.toString().getBytes();
				entity.setContent(content);
				bt.save(entity);
			} catch (IOException e) {
				sb.delete(0, sb.length());
			} finally {
				HttpUtils.closeStream(inStream);
			}
		} else {
			throw new Exception("conn exception");
		}
	}
}
